//
//  BMHDatePickerView.m
//  BMHVideoMain
//
//  Created by 陈宪栋 on 2017/3/29.
//  Copyright © 2017年 Baomihua. All rights reserved.
//

#import "BMHDatePickerView.h"
#import <ReactiveCocoa.h>
#import <UIView+Layout.h>
#import <UtilsMacro.h>

@interface BMHDatePickerView ()
@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) UIDatePicker *datePicker;
@property (nonatomic, strong) NSDateFormatter *formatter;
@property (nonatomic, strong) NSString *dateString;
@end

@implementation BMHDatePickerView

-(RACSubject *)dismissSignal{
    if (_dismissSignal == nil) {
        _dismissSignal = [[RACSubject alloc]init];
    }
    return _dismissSignal;
}

-(RACSubject *)sureSignal{
    if (_sureSignal == nil) {
        _sureSignal = [[RACSubject alloc]init];
    }
    return _sureSignal;
}

- (instancetype)initWithFrame:(CGRect)frame date:(NSDate *)birthday{
    self = [super initWithFrame:frame];
    if (self) {
        self.dateString = [self.formatter stringFromDate:birthday];
        
        self.contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height)];
        self.contentView.backgroundColor = RGB(237, 237, 237);
        [self addSubview:self.contentView];
        
        UIView *optionView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.contentView.width, 44)];
        optionView.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:optionView];
        
        UIButton *cancleBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 88, optionView.height)];
        [cancleBtn setTitle:@"取消" forState:UIControlStateNormal];
        [cancleBtn setTitleColor:RGB(199, 199, 205) forState:UIControlStateNormal];
        cancleBtn.titleLabel.font = [UIFont systemFontOfSize:18];
        @weakify(self);
        [[cancleBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self);
            [self dismiss];
        }];
        [optionView addSubview:cancleBtn];
        
        UIButton *sureBtn = [[UIButton alloc]initWithFrame:CGRectMake(optionView.width - cancleBtn.width, 0, cancleBtn.width, cancleBtn.height)];
        [sureBtn setTitle:@"完成" forState:UIControlStateNormal];
        [sureBtn setTitleColor:RGB(111, 157, 245) forState:UIControlStateNormal];
        sureBtn.titleLabel.font = [UIFont systemFontOfSize:18];
        [[sureBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self);
            [self.sureSignal sendNext:self.dateString];
            [self dismiss];
        }];
        [optionView addSubview:sureBtn];
        
        self.datePicker =  [[UIDatePicker alloc] initWithFrame:CGRectMake(0, optionView.bottom, self.contentView.width, self.contentView.height - 44)];
        self.datePicker.backgroundColor = [UIColor whiteColor];
        [self.datePicker setMinimumDate:[NSDate date]];
        [self.datePicker setDatePickerMode:UIDatePickerModeDateAndTime];
        [self.datePicker addTarget:self action:@selector(dateChange:) forControlEvents:UIControlEventValueChanged];
        if (birthday) {
            [self.datePicker setDate:birthday];
        } else {
            [self.datePicker setCalendar:[NSCalendar currentCalendar]];
        }
        [self.contentView addSubview:_datePicker];
        
    }
    return self;
}

- (void)dateChange:(UIDatePicker *)datePicker {
    self.dateString = [self.formatter stringFromDate:datePicker.date];
}

- (void)dismiss {
    [self.dismissSignal sendNext:nil];
    //
}

- (NSDateFormatter *)formatter {
    if (_formatter) {
        return _formatter;
    }
    _formatter = [[NSDateFormatter alloc] init];
    [_formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    return _formatter;
}

- (void)dealloc {
    BMHLog(@"%@ dealloc", [self class]);
}

@end
